package com.sdsy.util;

import org.apache.commons.io.FileUtils;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * Created by yxc on 2018/4/10.
 */
public class CommonUtil {


    /**
     * 根据合约的decimals转成需要的数量
     *
     * @param s
     * @return
     */
    public static BigInteger aDouble(String s) {
        double m = Double.valueOf(s);
        double mm = m * 100000000L;//
        BigDecimal bd = new BigDecimal(String.valueOf(mm));
        System.out.println(" ==========bd==================" + bd.toPlainString());
        BigInteger f = new BigInteger(bd.toPlainString());
        return f;
    }

    /**
     * 生成Credentials
     */
    public Credentials generateCredentials() {


        Credentials credentials = null;
        try {
            InputStream stream = getClass().getClassLoader().getResourceAsStream("D:\\UTC\\UTC--2018-02-27T04-00-37.998Z--0x02E784F79fb59249B51EFD166cf57f1D76A88900");
            File targetFile = new File("D:\\UTC");
            FileUtils.copyInputStreamToFile(stream, targetFile);
            credentials = WalletUtils.loadCredentials("111111111", targetFile);
            System.out.println("credentials=" + credentials.getAddress());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return credentials;
    }

    public static String generateFullNewWalletFile(String password, File file) throws CipherException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
        String str = WalletUtils.generateLightNewWalletFile(password, file);
        System.out.println(str);
        return str;
    }


    public static void main(String[] args) throws IOException {

        System.out.println(aDouble("1000"));
//        File targetFile = new File("D:\\work\\wallet-manage\\src\\main\\resources\\config");
//        String password = "123456789";
//        try {
//            generateFullNewWalletFile(password, targetFile);
//        } catch (CipherException e) {
//            e.printStackTrace();
//        } catch (InvalidAlgorithmParameterException e) {
//            e.printStackTrace();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        } catch (NoSuchProviderException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }


    /**
     * 判断是否为空
     *
     * @param
     * @return
     */
    public static boolean isNull(Object obj) {
        if (obj == null || "null".equalsIgnoreCase(String.valueOf(obj))) return true;
        else {
            if ("".equalsIgnoreCase(String.valueOf(obj).trim())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 生成唯一id
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    /**
     *
     */
    public static String formatTimeEight(String time) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date(Long.parseLong(time) * 1000L);
        Long date = d.getTime() - (8 * 60 * 60 * 1000);
        String newtime = sd.format(date);//把得到的新的时间戳再次格式化成时间的格式
        return newtime;
    }
}
